package borrower

import (
	"NovelBook/logic/account"
	"NovelBook/models/res"
	"NovelBook/utils/response"
	"github.com/gin-gonic/gin"
	"log"
	"net/http"
)

func LoginByAccount(c *gin.Context) {
	var user res.AccountPassword
	// 绑定前端数据
	err := c.ShouldBindJSON(&user)
	if err != nil {
		response.Fail(c, http.StatusBadRequest, response.CodeParamsInvalid)
		log.Println("bind err : ", err)
		return
	}

	// 调用账户登录
	userToken, err := account.LoginByAccount(user, account.BORROWER)
	if err != nil {
		response.FailWithMsg(c, http.StatusBadRequest, response.CodeParamsInvalid, err.Error())
		return
	}

	response.Success(c, userToken)
}

func LoginByEmail(c *gin.Context) {
	var user res.EmailPassword
	// 绑定前端数据
	err := c.ShouldBindJSON(&user)
	if err != nil {
		response.Fail(c, http.StatusBadRequest, response.CodeParamsInvalid)
		log.Println("bind err : ", err)
		return
	}

	// 调用邮箱登录
	userToken, err := account.LoginByEmail(user, account.BORROWER)
	if err != nil {
		response.FailWithMsg(c, http.StatusBadRequest, response.CodeParamsInvalid, err.Error())
		return
	}

	response.Success(c, userToken)
}
